package com.xupypr.surgery.server.objects;

import java.util.Date;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Text;
import com.xupypr.surgery.client.objects.BlogEntry;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class JDOBlogEntry {

	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Long id;

	@Persistent
	private Date created;

	@Persistent
	private Date lastModified;

	@Persistent
	private String caption;

	@Persistent()
	private Text text;

	@Persistent
	private Text cuttedText;

	@Persistent
	private Boolean publicated = false;

	public Boolean getPublicated() {
		return (publicated == null) ? false : publicated;
	}

	public void setPublicated(Boolean publicated) {
		this.publicated = publicated;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Date getCreated() {
		return (created == null) ? new Date(0) : created;
	}

	public void setCreated(Date created) {
		this.created = created;
	}

	public Date getLastModified() {
		return lastModified;
	}

	public void setLastModified(Date lastModified) {
		this.lastModified = lastModified;
	}

	public String getCaption() {
		return caption;
	}

	public void setCaption(String caption) {
		this.caption = caption;
	}

	public Text getText() {
		return text;
	}

	public void setText(Text text) {
		this.text = text;
	}

	public Text getCuttedText() {
		return cuttedText;
	}

	public void setCuttedText(Text cuttedText) {
		this.cuttedText = cuttedText;
	}

	public static BlogEntry fromPersistable(JDOBlogEntry object, boolean full) {
		BlogEntry result = new BlogEntry();
		result.setCaption(object.getCaption());
		result.setCreated(object.getCreated());
		result.setId(object.getId());
		result.setLastModified(object.getLastModified());
		result.setPublicated(object.getPublicated());
		result.setText(object.getText().getValue());
		if (full) {
			result.setLastModified(object.getLastModified());
			result.setCuttedText(object.getCuttedText().getValue());
		}
		return result;
	}

	public static JDOBlogEntry toPersistable(BlogEntry object, boolean full) {
		JDOBlogEntry result = new JDOBlogEntry();
		result.setCaption(object.getCaption());
		result.setCreated(object.getCreated());
		result.setId(object.getId());
		result.setPublicated(object.getPublicated());
		result.setText(new Text(object.getText()));
		if (full) {
			result.setCuttedText(new Text(object.getCuttedText()));
			result.setLastModified(object.getLastModified());
		}
		return result;
	}
}
